home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / vb / justify2.txt < prev    next >
Encoding:
Text File  |  1993-01-31  |  5.3 KB  |  123 lines

  1. This is the main text file with the form load and command1_click() routines
  2.  
  3. The purpose of this routine is to extract text from a VB trext box and justify the text to both the left and right margins on a line by line basis.
  4.  
  5. The main form is a form big enough to hold a Text Box 5760 Twips wide by 4320 Twips high.
  6. the form rewuires a Command Button called Command1. The MultiLine property of the text box should be set to TRUE
  7.  
  8. Sub Command1_Click ()
  9. 'What Happens When You Push The "Print Out Justify Text"
  10.  
  11. 'Printer Setup Dialog Box Variables
  12.    'PrintSet% = 5
  13.    'PrinterDialog.Action = PrintSet%
  14.    
  15. 'Note: the next routine to determine #lines and #characters/line
  16.    'was drawn from Article Number: Q84555 of the VB knowledgebase
  17.  
  18.  
  19. 'Find out how many lines and characters per line
  20.    'in TextBucket
  21.                  
  22.    WM_USER = &H400
  23.    EM_GETLINECOUNT = WM_USER + 10
  24.    EM_GETLINE = WM_USER + 20
  25.    Justify1.TextBucket.SetFocus
  26.    Texthwnd% = GetFocus()
  27.    NumLines% = SendMessage(Texthwnd%, EM_GETLINECOUNT, 0, 0&)
  28.  
  29. 'Find out how many Twips left over after printing this line of text at
  30.    'the specified FontName and FontSize
  31.    Printer.FontName = "Lucida Bright"
  32.    Printer.FontSize = 10
  33.    
  34.    For I% = 0 To NumLines% - 1
  35.       X$ = Space$(80) 'Setup required empty line variables (indexed)
  36.       CurrentChar$ = Space(1) 'Used to hold the current character
  37.  
  38.       NumChars% = SendMessage(Texthwnd%, EM_GETLINE, I%, X$)
  39.       SpaceCount% = 0
  40.       MakeUpWidth% = (RightPrintMargin - LeftPrintMargin) - (Printer.TextWidth(Trim$(X$)))
  41.       NumChars% = Len(Trim$(X$)) 'Get number Of characters in this line
  42.  
  43. 'Determine number of spaces in each line
  44.    'That's where your going to spread the makeup width
  45.       For j = 1 To NumChars%
  46.          CurrentChar$ = Mid$(LTrim$(X$), j, 1)
  47.          If CurrentChar$ = " " Then
  48.             SpaceCount% = SpaceCount% + 1
  49.          End If
  50.       Next j
  51.  
  52. 'Determine if the line requires makeup space
  53.       If SpaceCount% = 0 Then
  54.          MakeupSpace% = 0
  55.       Else
  56.          MakeupSpace% = (MakeUpWidth% / SpaceCount%) + Printer.TextWidth(" ")
  57.       End If
  58.       
  59.       CurrentChar$ = ""
  60.       Printer.CurrentX = LeftPrintMargin
  61.       'Printer.CurrentY = TextBucket.Top
  62.  
  63.       For j = 1 To NumChars%
  64.          CurrentChar$ = Mid$(LTrim$(X$), j, 1)
  65.          
  66.          If CurrentChar$ = " " Then
  67.             If Printer.TextWidth(Trim$(X$)) > ((RightPrintMargin - LeftPrintMargin) * .8) Then
  68.             'The .8 factor is somewhat arbitrary ... it is dependent upon text structure
  69.                'If you chose the entered words carefully so as to have minimal gap between
  70.                'the end of the last word and the right margin of the text box, this factor
  71.                'can be raise to .85 or .90
  72.                Printer.CurrentX = Printer.CurrentX + MakeupSpace%
  73.                Printer.Print Trim$(CurrentChar$);
  74.             Else
  75.                Printer.Print " ";
  76.             End If
  77.          Else
  78.             Printer.Print Trim$(CurrentChar$);
  79.          End If
  80.          
  81.       Next
  82.       Printer.CurrentY = Printer.CurrentY + Printer.TextHeight(CurrentChar$)
  83.       Printer.CurrentX = LeftPrintMargin
  84.    Next
  85.    Printer.EndDoc
  86. End
  87. End Sub
  88.  
  89. Sub Form_Load ()
  90.    Justify1.Left = LeftForm
  91.    Justify1.Top = TopForm
  92.    Justify1.Width = FormWidth
  93.    Justify1.Height = FormHeight
  94.  
  95.    TextBucket.Top = TopPrintMargin
  96.    TextBucket.Width = RightPrintMargin - LeftPrintMargin
  97.    TextBucket.Height = 4320
  98.    Command1.Left = LeftPrintMargin
  99.    Command1.Top = TextBucket.Top + TextBucket.Height + 180
  100.    Command1.Height = 720
  101.    TextBucket.FontName = "Lucida Bright"
  102.    TextBucket.FontSize = 10
  103. End Sub
  104.  
  105. This is the '.BAS' file with constants, API calls etc
  106.  
  107. 'GetFocus returns the handle of the window with the focus
  108. Declare Function GetFocus% Lib "User" ()
  109.  
  110. 'SendMessage sends a message to the specified window
  111.    'Note: the following declare should be on one line
  112. Declare Function SendMessage% Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal Iparam As Any)
  113.  
  114. 'Set left & right print margin parameters
  115. Global Const LeftForm = 1440
  116. Global Const TopForm = 1440
  117. Global Const FormWidth = 8640
  118. Global Const FormHeight = 7200
  119.  
  120. Global Const LeftPrintMargin = 720
  121. Global Const RightPrintMargin = 6480
  122. Global Const TopPrintMargin = 720
  123.                                                                                                                                                                                                                                                                                                                     Ç  ╠                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         Ç  ═  ╧  d  f  ┴  3  5  L  å  ê  ¡  └  µ  δ  -  p  r  t  ª  ╣  ╠  α    !  B  ]  £  ₧  µ    6  P  U  u  ╗  ■     >  U  ▒  ·  ⁿ  &  ^  |  ¼  ╤  ⁿ